var ctx;
var cellRegionId = "";
var cellBuildingId = "";
var cellUnitId = "";
var ownerHouseId = "";
var phoneReg = /^(13[0-9]|15[0-9]|18[0-9]|14[578])[0-9]{8}$/;

function setCtx(ctx) {
	this.ctx = ctx;
}

$(function(){
	initListener();  //添加区域监听事件
});
function initListener() {
	$("#cellRegionId").change(function () {
    	cellRegionId = $('#cellRegionId').val();
    	if (cellRegionId != ''){
    		$.ajax({
		        type : "GET",
		        url : ctx + "/ownerHouse/getCellBuildingByRegionId",
		        cache : false,
		        dataType : 'json',
		        data : {
		        	regionId : cellRegionId
		        },
		        success : function(data) {
		        	if (data.success) {
		        		var result = "<option value='" + "'>--楼栋--</option>";
		                $.each(data.obj, function(index, item) {
		                    result += "<option id='" + item.buildingName + "'" + " value='" + item.id + "'>" + item.buildingName + "</option>";
		                });
		                $("#cellBuildingId").html('');
		                $("#cellBuildingId").append(result);
		                $("#cellBuildingId").change();
		            }
		        }
			});
    	} else {
    		var result = "<option value='" + "'>--楼栋--</option>";
			$("#cellBuildingId").html('');
            $("#cellBuildingId").append(result);
            $("#cellBuildingId").change();
    	}
    });
    
    $("#cellBuildingId").change(function () {
    	cellBuildingId = $('#cellBuildingId').val();
    	if (cellBuildingId != ''){
    		$.ajax({
		        type : "GET",
		        url : ctx + "/ownerHouse/getCellUnitByBuildingId",
		        cache : false,
		        dataType : 'json',
		        data : {
		        	buildingId : cellBuildingId
		        },
		        success : function(data) {
		        	if (data.success) {
		        		var result = "<option value='" + "'>--单元--</option>";
		                $.each(data.obj, function(index, item) {
		                    result += "<option id='" + item.name + "'" + " value='" + item.id + "'>" + item.name + "</option>";
		                });
		                $("#cellUnitId").html('');
		                $("#cellUnitId").append(result);
		                $("#cellUnitId").change();
		            }
		        }
			});
    	} else {
    		var result = "<option value='" + "'>--单元--</option>";
			$("#cellUnitId").html('');
            $("#cellUnitId").append(result);
            $("#cellUnitId").change();
    	}
    });
    
    $("#cellUnitId").change(function () {
    	cellUnitId = $('#cellUnitId').val();
    	
    	if (cellUnitId != ''){
    		$.ajax({
		        type : "GET",
		        url : ctx + "/ownerHouse/getCellHouseByUnitId",
		        cache : false,
		        dataType : 'json',
		        data : {
		        	unitId : cellUnitId
		        },
		        success : function(data) {
		        	if (data.success) {
		        		var result = "<option value='" + "'>--房屋--</option>";
		                $.each(data.obj, function(index, item) {
		                    result += "<option id='" + item.name + "'" + " value='" + item.id + "'>" + item.name + "</option>";
		                });
		                $("#cellHouseId").html('');
		                $("#cellHouseId").append(result);
		                $("#cellHouseId").change();
		            }
		        }
			});
    	} else {
    		var result = "<option value='" + "'>--房屋--</option>";
			$("#cellHouseId").html('');
            $("#cellHouseId").append(result);
            $("#cellHouseId").change();
    	}
    });
    
    
    $('#cellHouseId').change(function() {
    	if ($('#cellHouseId').val() != 0) {
    		ownerHouseId = $('#cellHouseId').val();
    	}
    });

}

//验证收件人
function checkRecepientName(){
	var recepientName = $("#recepientName").val();
	//解决ie9以下placeholder与value重复的问题
	if(recepientName == $("#recepientName").attr("placeholder")){
		recepientName = "";
	}
	
	if(recepientName == null || recepientName == ""){
		layer.tips('收件人不能为空!', '#recepientName', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证区域不为空
function checkCellRegion(){
	var regionId = $("#cellRegionId").val();
	if(regionId == null || regionId == ""){
		layer.tips('请选择区域!', '#cellRegionId', {
			tips: 3,
		    time:1000
		});
	return false;
	}
	return true;
}

//验证楼栋不为空
function checkCellBuilding(){
	var buildingId = $("#cellBuildingId").val();
	
	if(buildingId == null || buildingId == ""){
		layer.tips('请选择楼栋!', '#cellBuildingId', {
			tips: 3,
		    time:1000
		});
		return false;
	}
	return true;
}

//验证单元不为空
function checkCellUnit(){
	var cellUnitId = $("#cellUnitId").val();
	
	if(cellUnitId == null || cellUnitId == ""){
		layer.tips('请选择单元!', '#cellUnitId', {
			tips: 3,
		    time:1000
		});
		return false;
	}
	return true;
}

//验证房屋不为空
function checkCellHouse(){
	var cellHouseId = $("#cellHouseId").val();
	
	if(cellHouseId == null || cellHouseId == ""){
		layer.tips('请选择房屋!', '#cellHouseId', {
			tips: 3,
		    time:1000
		});
		return false;
	}
	return true;
}

//验证手机号码
function checkPhone(){
	var phone = $("#phone").val();
	//解决ie9以下placeholder与value重复的问题
	if(phone == $("#phone").attr("placeholder")){
		phone = "";
	}
	if(phone == null || phone == ""){
		layer.tips('手机号码不能为空!', '#phone', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		if(!phoneReg.test(phone)){
			layer.tips('手机号码格式错误，请填写正确格式的手机号码!', '#phone', {
			    tips: 3,
			    time:1000
			});
			return false;
		}else{
			return true;
		}
	}
}
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
function saveAddress(){
	var addressId = $("#addressId").val();
	var recepientName = $("#recepientName").val();
	var cellRegionId = $("#cellRegionId").val();
	var cellUnitId = $('#cellUnitId').val();
	var cellBuildingId = $("#cellBuildingId").val();
	var cellHouseId = $("#cellHouseId").val();
	var phone = $("#phone").val();
	if($("#defaultAddress").get(0).checked){
		var defaultFlag = 1;
	} else {
		var defaultFlag = 0;
	}
	
	var ownerAddress = {};
	ownerAddress.id = addressId;
	ownerAddress.recepientName = recepientName;
	ownerAddress.regionId = cellRegionId;
	ownerAddress.unitId = cellUnitId;
	ownerAddress.buildingId = cellBuildingId;
	ownerAddress.houseId = cellHouseId;
	ownerAddress.recepientPhone = phone;
	ownerAddress.defaultFlag = defaultFlag;
	
	
	if (checkRecepientName() && checkCellRegion() && checkCellBuilding() && checkCellUnit() 
			&& checkCellHouse() && checkPhone()) {
		var index1 = layer.load(1);
		$.post(ctx +"/ownerAddress/saveOrUpdate", ownerAddress, function(data) {
			data = JSON.parse(data);
			if (data.success) {
				layer.msg("操作成功", {
					icon : 1,
					time : 1000
				// 2秒关闭（如果不配置，默认是3秒）
				}, function() {
					layer.close(index1);
					parent.layer.close(index);
//					window.location.href= ctx + "/ownerAddress/ownerAddressList";
				});
				return true;
			} 
		});
	} else {
		return false;
	}
}

function deleteAddress(addressId) {
	layer.confirm('确认删除?', {
		skin : 'demo-class',
		title : "<i style='font-size:18px;' >"
				+ "&#xe633;" + " </i >" + '确认'
	}, function(index) {
		$.post(ctx + "/ownerAddress/deleteOwnerAddress", {
			addressId : addressId
		}, function(data) {
			data = JSON.parse(data);
			if (data.success) {
				layer.msg("删除成功", {
					icon : 1,
					time : 1000
				// 2秒关闭（如果不配置，默认是3秒）
				}, function() {
					window.location.href = ctx + "/ownerAddress/ownerAddressList";
				});
			} else {
				layer.msg(data.msg, {
					icon : 2,
					time : 1000
				// 2秒关闭（如果不配置，默认是3秒）
				});
			}
		});
		layer.close(index);
	})
}